package com.education.module.system.sysUserRole;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.SysRole;
import com.education.domain.SysUserRole;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class SysUserRoleServices extends BaseServices implements IDao<SysUserRole>{

	@Override
	public List<SysUserRole> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT a.id,a.user_id,a.role_id,b.role_name,b.role_remark FROM sys_user_role a ");
		sql.append("inner join sys_role b on a.role_id = b.id ");
		sql.append("where a.user_id=? ");
		Object userId = searchParams.get("userid");
		List<Object> argsList = new ArrayList<Object>();
		argsList.add(userId);
		
		String lp = " and ";
		if(null != searchParams){
			if(null != searchParams.get("title") && !"".equals((String)searchParams.get("title"))){
				sql.append(lp).append(" title like ? ");
				argsList.add("%" + searchParams.get("title") + "%");
				lp = " and ";
			}
		}
		Object[] args = argsList.toArray();
		List<SysUserRole> list = dao.query(pageSQL(sql.toString(),page),args,new SysUserRoleRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from sys_user_role where user_id=?",new Object[]{userId}));
		return list;
	}
	
	public List<SysUserRole> find() {
		
		return null;
	}

	@Override
	public int save(SysUserRole obj) {
		 StringBuffer sql = new StringBuffer(); 
		 sql.append("insert into sys_user_role ( "); 
		 sql.append("id,user_id,role_id ");  
		 sql.append(" ) values(?,?,?) "); 
		 Object[] args = {obj.getId(),obj.getUserId(),obj.getRoleId() };
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public SysUserRole findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,user_id,role_id FROM sys_user_role ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new SysUserRoleRowmapper());
	}

	@Override
	public void update(SysUserRole obj) {
		 
		 StringBuffer sql = new StringBuffer(); 
		 sql.append("update sys_user_role "); 
		 sql.append("set  "); 
		 sql.append("user_id=?,role_id=? where id=?  "); 
		 Object[] args = {obj.getUserId(),obj.getRoleId(),obj.getId() };
				 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from sys_user_role where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class SysUserRoleRowmapper implements RowMapper<SysUserRole> {
		@Override
		public SysUserRole mapRow(ResultSet rs, int arg1) throws SQLException {
			SysUserRole obj = new SysUserRole();
			obj.setId(rs.getInt("id")); 
			obj.setRoleId(rs.getInt("role_id")); 
			obj.setUserId(rs.getInt("user_id")); 
			SysRole role = new SysRole();
			role.setRoleName(rs.getString("role_name"));
			role.setRoleRemark(rs.getString("role_remark"));
			obj.setRole(role);
			return obj;
		}
	}

}
